package com.crazymaker.springcloud.message.controller;

import com.crazymaker.push.platform.contract.dto.MsgDTO;
import com.crazymaker.springcloud.common.constants.SessionConstants;
import com.crazymaker.springcloud.common.exception.BusinessException;
import com.crazymaker.springcloud.common.result.RestOut;
import com.crazymaker.springcloud.message.service.impl.MessagePushServiceImpl;
import com.crazymaker.springcloud.message.service.impl.MessageRelayService;
import com.crazymaker.springcloud.message.service.impl.WebSocketServerMonitorService;
import com.crazymaker.springcloud.tunnel.remote.client.TunnelClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/crazymaker/message/")
@Api(tags = "消息管理")
@Slf4j
public class MessageController {
    @Resource
    MessagePushServiceImpl mqMessageService;

    @Resource
    MessageRelayService messageRelayService;

    @Resource
    WebSocketServerMonitorService webSocketServerMonitorService;

    @Resource
    TunnelClient tunnelClient;

    /**


     {
     "appId": 0,
     "appKey": "",
     "body": "巴巴版本不不bbbbbbbbbbbbbb",
     "msgId": 0,
     "targetIds": "1111111",
     "targetType": 1,
     "title": "吞吞吐吐ttttt"
     }
     * @param dto
     * @param request
     * @return
     */

    @PostMapping("/rpcPush/v1")
    @ApiOperation(value = "通过rpc调用发消息")
    public RestOut<String> rpcPush(@RequestBody MsgDTO dto, HttpServletRequest request) {

        String appId = request.getHeader(SessionConstants.USER_IDENTIFIER);
        if (null == appId) {
            throw BusinessException.builder().errMsg("参数不对，需要 appId").build();
        }
        dto.setAppId(Long.valueOf(appId));
        tunnelClient.pushByRpc(dto);

        return RestOut.success("发送完成");
    }


    /**

     {
     "appId": 0,
     "appKey": "",
     "body": "巴巴版本不不bbbbbbbbbbbbbb",
     "msgId": 1234553321,
     "targetIds": "1860000000",
     "targetType": 1,
     "title": "吞吞吐吐ttttt"
     }
     * @param dto
     * @param request
     * @return
     */

    @PostMapping("/push/v1")
    @ApiOperation(value = "发送消息")
    public RestOut<String> syncPush(@RequestBody MsgDTO dto, HttpServletRequest request) {

        String appId = request.getHeader(SessionConstants.USER_IDENTIFIER);
        if (null == appId) {
            throw BusinessException.builder().errMsg("参数不对，需要 appId").build();
        }
        dto.setAppId(Long.valueOf(appId));
        mqMessageService.saveAndDispatchMessage(dto);

        return RestOut.success("发送完成");
    }


    @PostMapping("/create/topic/v1")
    @ApiOperation(value = "创建topic")
    public RestOut<String> createTopic(@RequestBody String topic) {

        messageRelayService.createTopic(topic);

        return RestOut.success("创建完成");
    }


    @ApiOperation(value = "demo", notes = "提示接口使用者注意事项", httpMethod = "GET")
    @ApiImplicitParam(dataType = "string", name = "name", value = "姓名", required = true)
    @RequestMapping(value = "/hello")
    public String hello(String name) {
        log.info("方法 hello 被调用了");

        return "hello " + name;
    }

    @ApiOperation(value = "refresh ws gateway", notes = "刷新ws 网关", httpMethod = "GET")
    @RequestMapping(value = "/refreshWebsocketNodes")
    public String refreshWebsocketNodes() {
        log.info("方法 refreshWebsocketNodes 被调用了");

        String gateway = webSocketServerMonitorService.refreshWebsocketNodes();
        return "gateway :" + gateway;
    }

}
